{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hello World - Introduction to C# interactive tutorial\n",
    "\n",
    "*In this tutorial, you'll use Jupyter notebooks to learn C# interactively. You write C# code and see the results of compiling and running your code directly in the notebook.*\n",
    "\n",
    "It contains a series of lessons that begin with a \"Hello World\" program. These lessons teach you the fundamentals of the C# language.\n",
    "\n",
    "## Run your first C# program\n",
    "\n",
    "Run the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "Console.WriteLine(\"Hello World!\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Congratulations! You've run your first C# program. It's a simple program that prints the message \"Hello World!\". It used the `System.Console.WriteLine` method to print that message. `Console` is a type that represents the console window. `WriteLine` is a method of the `Console` type that prints a line of text to that text console.\n",
    "\n",
    "Let's move on and explore more. The rest of this lesson explores working with the `string` type, which represents text in C#. Like the `Console` type, the `string` type has methods. The `string` methods work with text.\n",
    "\n",
    "## Declare and use variables\n",
    "\n",
    "Your first program printed the `string` \"Hello World!\" on the screen.\n",
    "\n",
    "> As you explore C# (or any programming language), you'll make mistakes when you write code. The **compiler** will find those errors and report them to you. When the output contains error messages, look closely at the example code, and the code in the interactive window to see what to fix. That exercise will help you learn the structure of C# code.\n",
    "\n",
    "The first cell prints message. You can write more useful programs by using **variables**. A **variable** is a symbol you can\n",
    "use to run the same code with different values. Let's try it! Execute the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "string aFriend = \"Bill\";\n",
    "Console.WriteLine(aFriend);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first line declares a variable, `aFriend` and assigns it a value, \"Bill\". The second line rints out the name.\n",
    "\n",
    "You can assign different values to any variable you declare. You can change the name to one of your friends. Add these two lines in the above cell after the code you've already added:\n",
    "\n",
    "```csharp\n",
    "aFriend = \"Maira\";\n",
    "Console.WriteLine(aFriend);\n",
    "```\n",
    "\n",
    "Notice that the same line of code prints two different messages, based on the value stored in the `aFriend` variable. You may have also noticed that the word \"Hello\" was missing in the last two messages. Let's fix that now. Modify the lines that print the message to the following, and run the cells:\n",
    "\n",
    "```csharp\n",
    "Console.WriteLine(\"Hello \" + aFriend);\n",
    "```\n",
    "\n",
    "You've been using `+` to build strings from **variables** and **constant** strings. There's a better way. You can place a variable between `{` and `}` characters to tell C# to replace that text with the value of the variable. This is called *String interpolation*.\n",
    "\n",
    "If you add a `$` before the opening quote of the string, you can then include variables, like `aFriend`, inside the string between curly braces. Give it a try:\n",
    "\n",
    "```csharp\n",
    "Console.WriteLine($\"Hello {aFriend}\");\n",
    "```\n",
    "\n",
    "## Do more with strings\n",
    "\n",
    "You've been using a **method**, `System.Console.WriteLine`, to print messages. A **method** is a block of code that implements some action. It has a name, so you can access it.\n",
    "\n",
    "Suppose your strings have leading or trailing spaces that you don't want to display. You want to **trim** the spaces from the strings.\n",
    "The `System.String.Trim` method and related methods `System.String.TrimStart` and `System.String.TrimEnd` do that work. You can just use those methods to remove leading and trailing spaces. Try the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "string greeting = \"      Hello World!       \";\n",
    "Console.WriteLine($\"[{greeting}]\");\n",
    "\n",
    "string trimmedGreeting = greeting.TrimStart();\n",
    "Console.WriteLine($\"[{trimmedGreeting}]\");\n",
    "\n",
    "trimmedGreeting = greeting.TrimEnd();\n",
    "Console.WriteLine($\"[{trimmedGreeting}]\");\n",
    "\n",
    "trimmedGreeting = greeting.Trim();\n",
    "Console.WriteLine($\"[{trimmedGreeting}]\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The square brackets `[` and `]` help visualize what the `Trim`, `TrimStart` and `TrimEnd` methods do. The brackets show where whitespace starts and ends.\n",
    "\n",
    "This sample reinforces a couple of important concepts for working with strings. The methods that manipulate strings return new string objects rather than making modifications in place. You can see that each call to any of the `Trim` methods returns a new string but doesn't change the original message.\n",
    "\n",
    "There are other methods available to work with a string. For example, you've probably used a search and replace command in an editor or word processor before. The `System.String.Replace` method does something similar in a string. It searches for a substring and replaces it with different text. The `System.String.Replace` method takes two **parameters**. These are the strings between the parentheses. The first string is the text to search for. The second string is the text to replace it with. Try it for yourself. Add this code to the above cell:\n",
    "\n",
    "```csharp\n",
    "string sayHello = \"Hello World!\";\n",
    "Console.WriteLine(sayHello);\n",
    "sayHello = sayHello.Replace(\"Hello\", \"Greetings\");\n",
    "Console.WriteLine(sayHello);\n",
    "```\n",
    "\n",
    "Two other useful methods make a string ALL CAPS or all lower case. Try the following code in the preceding cell:\n",
    "\n",
    "```csharp\n",
    "Console.WriteLine(sayHello.ToUpper());\n",
    "Console.WriteLine(sayHello.ToLower());\n",
    "```\n",
    "\n",
    "## Search strings\n",
    "\n",
    "The other part of a *search and replace* operation is to find text in a string. You can use the  `System.String.Contains` method for searching. It tells you if a string contains a substring inside it. Try the following cell to explore `System.String.Contains`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "string songLyrics = \"You say goodbye, and I say hello\";\n",
    "Console.WriteLine(songLyrics.Contains(\"goodbye\"));\n",
    "Console.WriteLine(songLyrics.Contains(\"greetings\"));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `System.String.Contains` method returns a **boolean** value which tells you if the string you were searching for was found. A **boolean** stores either a `true` or a `false` value. When displayed as text output, they are capitalized: `True` and `False`, respectively. You'll learn more about **boolean** values in a later lesson.\n",
    "\n",
    "***Challenge***\n",
    "\n",
    "There are two similar methods, `System.String.StartsWith` and `System.String.EndsWith` that also search for sub-strings in a string. These find a substring at the beginning or the end of the string. Try to modify the previous sample to use `System.String.StartsWith` and `System.String.EndsWith` instead of `System.String.Contains`. Search for \"You\" or \"goodbye\" at the beginning of a string. Search for \"hello\" or \"goodbye\" at the end of a string.\n",
    "\n",
    "> Watch your punctuation when you test for the text at the end of the string. If the string ends with a period, you must check for a string that ends with a period.\n",
    "\n",
    "You should get `true` for starting with \"You\" and ending with \"hello\" and false for starting with or ending with \"goodbye\".\n",
    "\n",
    "Keep learning by trying the [numbers in C#](numbers.ipynb) lesson."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "C#",
   "pygments_lexer": "csharp",
   "version": "8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
